package com.fjec.market.controller;

import com.fjec.common.jxls.DataSixUpload;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.JsonUtil;
import com.fjec.common.ys.PaidHelper;
import com.fjec.market.feign.BaseClient;
import com.fjec.market.schedule.handler.YsRegisterResultQueryHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fjec.market.service.IInfoYsepayRegisterService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 052.银盛支付注册表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-10-30
 */
@Slf4j
@RestController
@RequestMapping("/")
public class InfoYsepayRegisterController {

    @Resource
    private IInfoYsepayRegisterService service;

    @Autowired
    private PaidHelper paidHelper;


    /**
     * 保存或更新个人银盛进件数据
     *
     * @param id              主键Id，非必填
     * @param custName        姓名
     * @param idCardNum       身份证号
     * @param bankType        银行类型
     * @param provMap         省
     * @param cityMap         市
     * @param countyMap       区
     * @param address         详细地址
     * @param bankProvMap     开户行省
     * @param bankCityMap     开户行市
     * @param bankMap         开户行
     * @param bankCardNum     银行卡号
     * @param bankPhone       银行预留手机号
     * @param id102
     * @param url102
     * @param id103
     * @param url103
     * @param id104
     * @param url104
     * @param id105
     * @param url105
     * @param id106
     * @param url106
     * @param id107
     * @param url107
     * @param legalCertExpire 身份证有效期
     * @return
     */
    @PostMapping("/y/save/or/update/ys/personal/reg")
    public JsonResult saveOrUpdatePersonalRegInfo(@RequestParam(required = false) Long id,
                                                  @RequestParam String custName,
                                                  @RequestParam String idCardNum,
                                                  @RequestParam String bankType,
                                                  @RequestParam String provMap,
                                                  @RequestParam String cityMap,
                                                  @RequestParam String countyMap,
                                                  @RequestParam String address,
                                                  @RequestParam String bankProvMap,
                                                  @RequestParam String bankCityMap,
                                                  @RequestParam String bankMap,
                                                  @RequestParam String bankCardNum,
                                                  @RequestParam String bankPhone,
                                                  @RequestParam Long id102,
                                                  @RequestParam String url102,
                                                  @RequestParam Long id103,
                                                  @RequestParam String url103,
                                                  @RequestParam Long id104,
                                                  @RequestParam String url104,
                                                  @RequestParam Long id105,
                                                  @RequestParam String url105,
                                                  @RequestParam Long id106,
                                                  @RequestParam String url106,
                                                  @RequestParam String id107,
                                                  @RequestParam String url107,
                                                  @RequestParam String legalCertExpire) {
        JsonResult jsonResult;
        if (id == null || id == 0) {
            // 新增
            jsonResult = service.savePersonalRegInfo(custName, idCardNum, bankType, provMap, cityMap, countyMap, address, bankProvMap,
                    bankCityMap, null, bankMap, bankCardNum, bankPhone, id102, url102, id103, url103, id104, url104,
                    id105, url105, id106, url106, id107, url107, legalCertExpire);
        } else {
            // 更新
            jsonResult = service.updatePersonalRegInfo(id, custName, idCardNum, bankType, provMap, cityMap, countyMap, address, bankProvMap,
                    bankCityMap, null, bankMap, bankCardNum, bankPhone, id102, url102, id103, url103, id104, url104,
                    id105, url105, id106, url106, id107, url107, legalCertExpire);
        }
        return jsonResult;
    }

    /**
     * 保存或更新企业银盛进件数据
     *
     * @param id               主键Id，非必填
     * @param custName         公司名称
     * @param busLicense       有效营业执照号
     * @param busLicenseExpire 营业执照有效期yyyyMMdd
     * @param provMap          省
     * @param cityMap          市
     * @param countyMap        区
     * @param address          详细地址
     * @param legalName        法人姓名
     * @param legalCertNo      法人身份证号
     * @param legalTel         法人电话号码
     * @param legalCertExpire  法人身份证有效期
     * @param bankProvMap      开户行所在省
     * @param bankCityMap      开户行所在市
     * @param bankMap          银行信息
     * @param bankType         银行行别
     * @param bankCardNum      银行卡号
     * @param bankAccountName  开户人姓名
     * @param certNo           开户人身份证号
     * @param bankPhone        开户预留电话号
     * @param id102
     * @param url102
     * @param id103
     * @param url103
     * @param id106
     * @param url106
     * @param id107
     * @param url107
     * @param id108
     * @param url108
     * @param samePerson       开户人是否是法人
     * @return
     */
    @PostMapping("/y/save/or/update/ys/ent/reg")
    public JsonResult saveOrUpdateEntRegInfo(@RequestParam(required = false) Long id,
                                             @RequestParam String custName,
                                             @RequestParam String busLicense,
                                             @RequestParam String busLicenseExpire,
                                             @RequestParam String provMap,
                                             @RequestParam String cityMap,
                                             @RequestParam String countyMap,
                                             @RequestParam String address,
                                             @RequestParam String legalName,
                                             @RequestParam String legalCertNo,
                                             @RequestParam String legalTel,
                                             @RequestParam(required = false) String legalCertExpire,
                                             @RequestParam String bankProvMap,
                                             @RequestParam String bankCityMap,
                                             @RequestParam String bankMap,
                                             @RequestParam String bankType,
                                             @RequestParam String bankCardNum,
                                             @RequestParam(required = false) String bankAccountName,
                                             @RequestParam(required = false) String certNo,
                                             @RequestParam String bankPhone,
                                             @RequestParam Long id101,
                                             @RequestParam String url101,
                                             @RequestParam Long id102,
                                             @RequestParam String url102,
                                             @RequestParam Long id103,
                                             @RequestParam String url103,
                                             @RequestParam Long id106,
                                             @RequestParam String url106,
                                             @RequestParam String id107,
                                             @RequestParam String url107,
                                             @RequestParam Long id108,
                                             @RequestParam String url108,
                                             @RequestParam boolean samePerson) {
        JsonResult jsonResult;
        if (id == null || id == 0) {
            // 保存
            jsonResult = service.saveEntRegInfo(custName, busLicense, busLicenseExpire, provMap, cityMap, countyMap, address,
                    legalName, legalCertNo, legalTel, legalCertExpire, bankProvMap, bankCityMap, null,
                    bankMap, bankType, bankCardNum, bankAccountName, certNo, bankPhone, id101, url101, id102, url102, id103, url103,
                    id106, url106, id107, url107, id108, url108, samePerson);
        } else {
            // 更新
            jsonResult = service.updateEntRegInfo(id, custName, busLicense, busLicenseExpire, provMap, cityMap, countyMap, address,
                    legalName, legalCertNo, legalTel, legalCertExpire, bankProvMap, bankCityMap, null,
                    bankMap, bankType, bankCardNum, bankAccountName, certNo, bankPhone, id101, url101, id102, url102, id103, url103,
                    id106, url106, id107, url107, id108, url108, samePerson);
        }
        return jsonResult;
    }

    /**
     * 获取用户进件信息
     *
     * @param getType 1=返回图片信息 其他=不返回图片信息
     * @return 脱敏规则：
     * 身份证号保留前3位和后4位
     * 银行卡号保留最后4位
     * 手机号保留前3位和后4位
     * <p>
     * 其余星号代替
     * @throws Exception
     */
    @GetMapping("/y/get/ys/reg/info")
    public JsonResult getYsRegInfo(@RequestParam(required = false) Integer getType) throws Exception {
        return service.getYsRegInfo(getType);
    }

    /**
     * 获取进件状态
     * <p>
     * 最后进件状态regStatus
     * 1=未进件
     * 2=进件中
     * 3=进件成功
     * 4=进件失败
     * <p>
     * 最后进件文字结果regResult
     *
     * @return
     */
//    @GetMapping("/y/get/ys/reg/status")
//    public JsonResult getRegStatus() {
//        return service.getYsRegStatus();
//    }


    /**
     * 银盛进件回调
     *
     * @param map
     * @return
     */
    @PostMapping("/n/ys/notify/reg/post")
    public String ysRegNotifyPost(@RequestBody Map<String, Object> map) {
        // todo: 异步获取到进件结果通知后
        String usercode = MapUtils.getString(map, "usercode");
        String custname = MapUtils.getString(map, "custname");
        String custid = MapUtils.getString(map, "custid");
        String user_status = MapUtils.getString(map, "user_status");
        String cust_status = MapUtils.getString(map, "cust_status");
        String merchant_no = MapUtils.getString(map, "merchant_no");
        log.error("回调Post！！！！！！！");
        service.ysRegNotify(usercode, custname, custid, user_status, cust_status, merchant_no);
        return "success";
    }

    @GetMapping("/n/ys/notify/reg/get")
    public String ysRegNotifyGet(@RequestBody Map<String, Object> map) {
        // todo: 异步获取到进件结果通知后
        String usercode = MapUtils.getString(map, "usercode");
        String custname = MapUtils.getString(map, "custname");
        String custid = MapUtils.getString(map, "custid");
        String user_status = MapUtils.getString(map, "user_status");
        String cust_status = MapUtils.getString(map, "cust_status");
        String merchant_no = MapUtils.getString(map, "merchant_no");
        log.error("回调Get！！！！！！！");
        service.ysRegNotify(usercode, custname, custid, user_status, cust_status, merchant_no);
        return "success";
    }

    /**
     * 银盛进件
     *
     * @param id
     * @return
     */
    @PostMapping("/ys/reg")
    public JsonResult ysReg(Long id) throws Exception {
        service.ysReg(id);
        return JsonResult.SUCCESS;
    }

    /**
     * 进件结果查询
     *
     * @return
     */
    @GetMapping("/ys/search/reg/by/userCode")
    public JsonResult searchYsReg(String userCode) throws Exception {
        String str = paidHelper.searchRegisterByUserCode(userCode);
        return JsonResult.success().data(str).build();
    }

    /**
     * 进件结果查询
     *
     * @param merchantNo
     * @return
     * @throws Exception
     */
    @GetMapping("/ys/search/reg/by/merchantNo")
    public JsonResult searchYsRegByMerchantNo(String merchantNo) throws Exception {
        return service.searchYsReg(merchantNo);
    }

    @GetMapping("/ys/download/xieyi")
    public void testXieYi(String custType, HttpServletResponse response) throws Exception {
        // 生成合同
        byte[] contract = service.generateContract(custType, "测试1", "测试2", "测试3");
        OutputStream out = response.getOutputStream();
        out.write(contract);
    }

    /**
     * 获取指定一天的线上进件成功列表
     *
     * @param dateTime
     * @return
     * @throws Exception
     */
    @PostMapping("/ys/offline/reg/list")
    public List<Map<String, Object>> getOffLineRegList(@RequestParam String dateTime) throws Exception {
        return service.getOffLineRegList(dateTime);
    }

    /**
     * 更新线下进件状态
     *
     * @param dataList
     * @return
     */
    @PostMapping("/ys/offline/reg/result")
    public Map<String, Object> updateOffLineRegResult(@RequestBody List<DataSixUpload> dataList) {
        return service.updateOffLineRegResult(dataList);
    }

}
